<?php

namespace App\Http\Controllers\Home;

use Illuminate\Http\Request;
use DB;
use App\Topic;
use App\Apraise;
use App\User;
use App\Topfocus;
use App\Questionfocus;

use App\Http\Requests;
use App\Http\Controllers\Controller;

class AjaxController extends Controller
{
    // 获得话题的相关信息
    public function topic(Request $request)
    {
        $topic_id = $request->input('topic_id');
        $topic = Topic::findOrFail($topic_id);
        $topic_name = $topic->topic_name;
        $arr[] = $topic_name;
        $topic_summary = $topic->topic_summary;
        $arr[] = $topic_summary;
        $topic_portrait = $topic->topic_portrait;
        // 调用Topic中的方法得到关注的人数
        $total = $topic->getFocusByTopic->count();
        // 调用Topic中的getQuestionByTopic方法得到该话题下的所有问题
        $questions = $topic->getQuestionByTopic->count();
        $arr[] = $total;
        $arr[] = $questions;
        $arr[] = $topic_portrait;

        // 通过session ,$user_id,判断该用户的关注话题
        $user_id = session('user_id');
        $user = User::findOrFail($user_id);
        $topics = [];
        foreach ($user->getTopfocus as $v) {
            $topics[] = $v->id;
        }
        if (in_array($topic_id, $topics)) {
            $return = 1;
        }else {
            $return = 0;
        }
        $arr[] = $return;
        echo json_encode($arr);

    }

    // 处理对问题回答的点赞
    public function apraise(Request $request)
    {
        $answer_id = $request->input('answer_id');

        $user_id = session('user_id');   //在这获取session $user_id

        $res = Apraise::where('user_id', $user_id)->where('answer_id', $answer_id)->first();

        if ($res) {
            if ($res->apraise_status == 1) {
                $res->apraise_status = 0;
                $return = 1; // 表明取消赞同
            } else {
                $res->apraise_status = 1;
                $return = 2; // 表明赞同
            }
        } else {
            $res = new Apraise;
            $res->apraise_status = 1;
            $res->user_id = $user_id;
            $res->answer_id = $answer_id;
            $return = 3; // 表明赞同
        }
        if($res->save()) {
            echo $return;
        }else{
            echo 0;
        }
    }

    //处理反对
    public function opisit(Request $request)
    {
        $answer_id = $request->input('answer_id');
        $user_id = session('user_id');
        $res = Apraise::where('answer_id', $answer_id)->where('user_id', $user_id)->first();
        if ($res) {
            if ($res->apraise_status == -1) {
                $return = 1; //该用户已经反对
            }elseif($res->apraise_status == 1) {
                $res->apraise_status = -1;
                $return = 2; //该用户当前赞同 ,现在反对
            }else{
                $res->apraise_status = -1;
                $return = 4;
            }
        }else {
            $res = new Apraise;
            $res->user_id = $user_id;
            $res->answer_id = $answer_id;
            $return = 3; //该用户当前没有反对也没有赞同 ,现在反对
        }
        if($res->save()) {
            echo $return;
        }else {
            echo 0;
        }
    }

    // 对悬浮框的话题进行关注或取消关注
    public function attention(Request $request)
    {
        $topic_id = $request->input('topic_id');
        $logo = $request->input('logo');
        $user_id = session('user_id');
        // 判断是执行关注或者取消关注
        if ($logo == 'del') {
            $fight = Topfocus::where('user_id', $user_id)->where('topic_id', $topic_id)->delete();
            $return = 1;
            echo $return;
        }elseif ($logo == 'add') {
            $fight = new Topfocus;
            $fight->user_id = $user_id;
            $fight->topic_id = $topic_id;
            $return  = 2;
            if ($fight->save()) {
                echo $return;
            }
        }
    }

    //  对问题的关注
    public function questionAttention(Request $request)
    {
        $qid = $request->input('qid');
        $user_id = session('user_id');
        $logo = $request->input('logo');
        // 判断是执行关注或者取消关注
        if ($logo == 'del') {
            $fight = Questionfocus::where('user_id', $user_id)->where('question_id', $qid)->delete();
            // $return = 2;
            // echo $return;
            $arr = [];
            $arr['num'] = 2;
            $arr['user_id'] = $user_id;
            echo json_encode($arr); 
        }

        if ($logo == 'add') {
            $fight = new Questionfocus;
            $fight->user_id = $user_id;
            $fight->question_id = $qid;
            $fight->create_time = time();
            // $return = 1;
            $arr = [];
            $arr['num'] = 1;
            $arr['user_id'] = $user_id;
            if ($fight->save()) {
                echo json_encode($arr); 
            }
        }
    }

    //  获取回答者的悬浮框
    public function people(Request $request)
    {
        $id = $request->input('id');
        $user = User::findOrFail($id);
        $num = $user->getAnswer->count();
        echo $num;
    }

}
